package com.ruoyi.supermarket.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.supermarket.mapper.SupermarketComShelfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.SupermarketCommodityMapper;
import com.ruoyi.supermarket.domain.SupermarketCommodity;
import com.ruoyi.supermarket.service.ISupermarketCommodityService;

/**
 * 超市商品信息Service业务层处理
 * 
 * @author faye
 * @date 2024-05-25
 */
@Service
@Slf4j
public class SupermarketCommodityServiceImpl implements ISupermarketCommodityService 
{
    @Autowired
    private SupermarketCommodityMapper supermarketCommodityMapper;

    @Autowired
    private SupermarketComShelfMapper comShelfMapper;
    /**
     * 查询超市商品信息
     * 
     * @param commodityId 超市商品信息主键
     * @return 超市商品信息
     */
    @Override
    public SupermarketCommodity selectSupermarketCommodityByCommodityId(Long commodityId)
    {
        SupermarketCommodity testcom=supermarketCommodityMapper.selectSupermarketCommodityByCommodityId(1001L);
        //System.out.println(testcom.getCommodityImage());

        return supermarketCommodityMapper.selectSupermarketCommodityByCommodityId(commodityId);
    }

    /**
     * 查询超市商品信息列表
     * 
     * @param supermarketCommodity 超市商品信息
     * @return 超市商品信息
     */
    @Override
    public List<SupermarketCommodity> selectSupermarketCommodityList(SupermarketCommodity supermarketCommodity)
    {

        return supermarketCommodityMapper.selectSupermarketCommodityList(supermarketCommodity);
    }

    /**
     * 新增超市商品信息
     * 
     * @param supermarketCommodity 超市商品信息
     * @return 结果
     */
    @Override
    public int insertSupermarketCommodity(SupermarketCommodity supermarketCommodity)
    {
        try{
            if(supermarketCommodity.getQuantity()<0){
                throw new IllegalArgumentException("不合法库存数!");
            }
        }catch (Exception e){
            supermarketCommodity.setQuantity(0L);
            log.info("商品插入时库存数量异常，为负数");
           // throw new IllegalArgumentException("不合法库存数!");
        }

        try {
            if(supermarketCommodity.getPlanValue()<supermarketCommodity.getWarnValue()){
                throw new IllegalArgumentException("计划库存不能小于预警库存！");
            }
        }catch (Exception e){
            //记录日志
            log.info("商品插入时计划库存与预警库存关系异常: {}", e.getMessage());
            //throw new IllegalArgumentException("计划库存不能小于预警库存！");
        }
        try {
            if(supermarketCommodity.getCost()>supermarketCommodity.getUnitPrice()){
                throw new IllegalArgumentException("进价不能大于单价！");
            }
        }catch (Exception e){
            //记录日志
            log.info("商品插入时进价与单价关系异常: {}", e.getMessage());
            //throw new IllegalArgumentException("进价不能大于单价！");
        }


        supermarketCommodity.setCreateTime(DateUtils.getNowDate());

        return supermarketCommodityMapper.insertSupermarketCommodity(supermarketCommodity);
    }

    /**
     * 修改超市商品信息
     * 
     * @param supermarketCommodity 超市商品信息
     * @return 结果
     */
    @Override
    public int updateSupermarketCommodity(SupermarketCommodity supermarketCommodity)
    {
        supermarketCommodity.setUpdateTime(DateUtils.getNowDate());
        Long plan=supermarketCommodity.getPlanValue(),warn=supermarketCommodity.getWarnValue();
        SupermarketCommodity oldCom=supermarketCommodityMapper.selectSupermarketCommodityByCommodityId(supermarketCommodity.getCommodityId());
        if(plan<0){
           supermarketCommodity.setPlanValue(oldCom.getPlanValue());
        }
        if(warn<0){
            supermarketCommodity.setWarnValue(oldCom.getWarnValue());
        }
        if(plan<=warn){
            supermarketCommodity.setPlanValue(oldCom.getPlanValue());
            supermarketCommodity.setWarnValue(oldCom.getWarnValue());

            supermarketCommodity.setRemark("预警量或计划量修改不正确，计划小于预警,保持原值");
            log.info("预警量或计划量修改不正确，计划小于预警");
        }


        return supermarketCommodityMapper.updateSupermarketCommodity(supermarketCommodity);
    }

    /**
     * 批量删除超市商品信息
     * 
     * @param commodityIds 需要删除的超市商品信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketCommodityByCommodityIds(Long[] commodityIds)
    {
        return supermarketCommodityMapper.deleteSupermarketCommodityByCommodityIds(commodityIds);
    }

    /**
     * 删除超市商品信息信息
     * 
     * @param commodityId 超市商品信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketCommodityByCommodityId(Long commodityId)
    {
        return supermarketCommodityMapper.deleteSupermarketCommodityByCommodityId(commodityId);
    }
}
